<!--
 * @Author: quit
 * @Date: 2021-11-16 17:49:05
 * @LastEditTime: 2021-12-14 14:35:06
 * @Descripttion: 用户项目管理
-->
<template>
  <div id="ProjectManagement-content">
    <div class="app-container">
      <el-card>
        <el-row class="row1">
          <el-divider content-position="left">{{ $t('divider.divider1') }}</el-divider>
          <div class="search">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
              <el-form-item :label="'用户姓名'">
                <el-input v-model="formInline.name" :placeholder="'请输入用户姓名'" />
              </el-form-item>
              <!-- <el-form-item :label="'用户性别'">
                <el-input v-model="formInline.user" :placeholder="$t('input.searchPlaceholder')" />
              </el-form-item> -->
              <el-form-item>
                <el-button type="primary" icon="el-icon-zoom-in" @click="getUserPMList">{{ $t('button.search') }}</el-button>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-refresh-right" @click="refresh">{{ '重置' }}</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-row>
        <el-row>
          <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="addItemsBtn">{{ $t('button.addItems') }}</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="batchRemove">{{ $t('button.batchRemove') }}</el-button>
        </el-row>
        <el-row>
          <el-table ref="table" v-loading="loading" :data="tableData" :empty-text="$t('table.emptyText')" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" min-width="40" fixed="left" />
            <el-table-column prop="id" label="ID" min-width="45" fixed="left" />
            <el-table-column prop="username" :label="$t('table.username')" min-width="120" fixed="left" />
            <el-table-column prop="appname" :label="'项目名'" min-width="100" />
            <el-table-column prop="powergroupname" :label="'项目权限组'" min-width="100" />
            <el-table-column prop="isShiftover" :label="'是否交接班'" min-width="80">
              <template slot-scope="scope">
                <span v-if="scope.row.isShiftover === 0">不参与</span>
                <span v-if="scope.row.isShiftover === 1">参与</span>
              </template>
            </el-table-column>
            <el-table-column header-align="center" align="center" fixed="right" :label="$t('table.operable')" min-width="100">
              <template slot-scope="scope">
                <el-button type="warning" size="mini" @click="handleEdit(scope.row)">{{ $t('button.edit') }}</el-button>
                <el-button type="danger" size="mini" @click="handleDelete(scope.row)">{{ $t('button.delete') }}</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page="pages.page"
            :page-sizes="[1, 5, 10]"
            :page-size="pages.size"
            layout="prev, pager, next, total, sizes, jumper"
            :total="pages.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 添加弹出层 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogAddVisible" width="30%" @close="dialogAddVisibleClose">
      <el-form ref="dialogFrom" :model="dialogFrom" label-width="100px" class="dialog-from">
        <el-form-item label="用户">
          <el-select v-model="dialogFrom.userid" clearable filterable placeholder="请选择用户">
            <el-option v-for="(item, index) in userList" :key="index" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目">
          <el-select v-model="dialogFrom.appid" placeholder="请选择项目类型" clearable>
            <el-option v-for="(item, index) in projectList" :key="index" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="权限组">
          <el-select v-model="dialogFrom.powergroupid" placeholder="请选择权限组" clearable>
            <el-option v-for="(item, index) in permissionData" :key="index" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否交接班">
          <el-select v-model="dialogFrom.isshiftover" placeholder="请选择是否交接班" clearable>
            <el-option v-for="(item, index) in isshiftoverData" :key="index" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogAddVisible = false">取 消</el-button>
        <el-button v-if="dialogAddVisibleState === 0" type="primary" @click="sendAdditem">确 定</el-button>
        <el-button v-if="dialogAddVisibleState === 1" type="primary" @click="sendEditItem">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserPMList, deleteUserPMList } from '@/api/BmRequest'

export default {
  components: {},
  data() {
    return {
      formInline: { name: null },
      loading: false,
      tableData: [],
      checkedArr: [],
      pages: { page: 1, size: 10, total: 0 },
      dialogTitle: null,
      dialogAddVisible: false,
      dialogAddVisibleState: null,
      dialogFrom: { userid: 12, appid: 14, powergroupid: 0, isshiftover: null },
      userList: [],
      projectList: [],
      permissionData: [],
      isshiftoverData: [
        { label: '不参与', value: 0 },
        { label: '参与', value: 1 }
      ]
    }
  },
  // 监听属性 类似于data概念F
  computed: {},
  // 监控data中的数据变化
  watch: {},
  created() {
    this.getUserPMList()
  }, // 生命周期 - 创建完成（可以访问当前this实例）
  mounted() {}, // 生命周期 - 挂载完成（可以访问DOM元素）
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
  // 方法集合
  methods: {
    // 获取table数据列表
    async getUserPMList() {
      this.loading = true
      const obj = {
        name: this.formInline.name,
        pages: {
          page: this.pages.page,
          size: this.pages.size
        }
      }
      try {
        const { data } = await getUserPMList(obj)
        this.tableData = data.list
        this.pages.page = data.pageNum
        this.pages.size = data.pageSize
        this.pages.total = data.total
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    },

    // 重置按钮
    refresh() {
      this.formInline = { name: null }
      this.pages = { page: 1, size: 10, total: 0 }
      this.getUserPMList()
    },

    // 表格多选事件
    async handleSelectionChange(val) {
      const arr = []
      val.forEach((item, i) => {
        arr.push(item.id)
      })
      this.checkedArr = arr
    },

    // 添加按钮
    addItemsBtn() {
      this.dialogAddVisibleState = 0
      this.dialogTitle = '添加用户项目信息'
      this.dialogAddVisible = true
    },

    // 批量删除
    batchRemove() {
      if (this.checkedArr.length > 0) {
        this.$confirm('此操作将删除所选项目, 是否继续?', '确认操作', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            if (this.checkedArr.length === this.tableData.length) {
              if (this.pages.page > 1) {
                this.pages.page--
              } else {
                this.pages.page = 1
              }
            }
            const { message } = await deleteUserPMList(this.checkedArr)
            await this.getUserList()
            this.$message.success({ message })
          })
          .catch(error => {
            console.log(error)
            this.$refs.table.clearSelection()
          })
      } else {
        this.$message.warning('请选择需要删除的项目')
      }
    },

    /* 弹出层事件 */
    // 获取用户数据
    // 获取项目数据
    // 关闭事件
    dialogAddVisibleClose() {
      this.dialogAddVisibleState = null
      this.dialogTitle = null
      this.dialogFrom = { userid: 12, appid: 14, powergroupid: 0, isshiftover: null }
    },
    // 确定
    sendAdditem() {},
    // 提交
    sendEditItem() {},

    /* table-item 操作 */
    // 编辑
    handleEdit(data) {
      console.log(data)
      const obj = { userid: data.id, appid: 14, powergroupid: 0, isshiftover: data.isShiftover }
      this.dialogFrom = { ...obj }
      this.dialogAddVisibleState = 1
      this.dialogTitle = '编辑用户项目信息'
      this.dialogAddVisible = true
    },
    // 删除
    handleDelete(data) {
      this.$confirm('此操作将删除当前项目, 是否继续?', '确认操作', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const Arr = [data.id]
          const { message } = await deleteUserPMList(Arr)
          if (this.tableData.length === 1) {
            if (this.pages.page > 1) {
              this.pages.page--
            } else {
              this.pages.page = 1
            }
          }
          await this.getUserList()
          this.$message.success({ message })
        })
        .catch(error => {
          console.log(error)
        })
    },
    /* 分页操作 */
    // 分页当前页切换
    async handleCurrentChange(newPage) {
      this.pages.page = newPage
      await this.getUserPMList()
    },
    // 分页显示条数切换
    async handleSizeChange(newSize) {
      this.pages.size = newSize
      await this.getUserPMList()
    }
  }
}
</script>

<style scoped>
.el-table {
  margin-top: 10px;
}
.el-form {
  margin-left: 20px;
}
.el-pagination {
  margin-top: 10px;
}
.el-input {
  width: 300px;
}
</style>

<style lang="scss" scoped>
.row1 {
  margin-bottom: 10px;
  .search {
    border: 1px solid #dcdfe6;
    border-top: none;
    padding: 34px 10px 10px 10px;
    margin-top: -14px;
  }
}
.dialog-from {
  .el-input,
  .el-select {
    width: calc(100% - 10px);
  }
}
</style>
